package com.sztx.worker_monitor.web.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.sztx.se.core.quartz.config.QuartzParameter;
import com.sztx.worker_monitor.common.DateStringUtils;
import com.sztx.worker_monitor.web.handle.CommonHandle;

public class QuartzModel {
	
	
	
	/**
	 * sched名称
	 */
	private String schedName;

	/**
	 * 任务名称
	 */
	private String jobName;

	/**
	 * 任务组
	 */
	private String jobGroup;

	/**
	 * 任务的具体类名, 可以是全路径的java类, 也可以是被spring管理的简单类名
	 */
	private String jobClassName;

	/**
	 * 任务的描述
	 */
	private String description;

	/**
	 * 任务中断（如断电）是否是重新执行
	 */
	private String isRecovery;

	/**
	 * 触发器的名称（如：每天2点触发库存对账）
	 */
	private String triggerName;

	/**
	 * 触发器分组
	 */
	private String triggerGroup;

	/**
	 * 是否是cron的表达式
	 */
	private String isCronTrigger;

	/**
	 * 任务触发时间的表达式(isCronTrigger等于true时为cron的表达式，isCronTrigger等于false时为间隔时间，单位秒)
	 */
	private String expression;
	
	private String cronDescription;

	/**
	 * 上次触发时间
	 */
	private String prevFireTime;

	/**
	 * 下次触发时间
	 */
	private String nextFireTime;

	/**
	 * 任务开始时间
	 */
	private String startAt;

	/**
	 * 任务结束时间
	 */
	private String endAt;

	/**
	 * 触发器状态
	 */
	private String status;

	/**
	 * 自定义扩展信息
	 */
	private Map<String, String> extraInfo;

	public QuartzModel(){}
	
	
	public QuartzModel(String schedName, String jobName, String jobGroup, String jobClassName, String description,
			String isRecovery, String triggerName, String triggerGroup, String isCronTrigger, String expression,
			String prevFireTime, String nextFireTime, String startAt, String endAt, String status,
			Map<String, String> extraInfo) {
		super();
		this.schedName = schedName;
		this.jobName = jobName;
		this.jobGroup = jobGroup;
		this.jobClassName = jobClassName;
		this.description = description;
		this.isRecovery = isRecovery;
		this.triggerName = triggerName;
		this.triggerGroup = triggerGroup;
		this.isCronTrigger = isCronTrigger;
		this.expression = expression;
		this.prevFireTime = prevFireTime;
		this.nextFireTime = nextFireTime;
		this.startAt = startAt;
		this.endAt = endAt;
		this.status = status;
		this.extraInfo = extraInfo;
		this.cronDescription = CommonHandle.getExDes(this.expression);
	}


	public QuartzModel(QuartzParameter quartzParameter){
		this.schedName 	= quartzParameter.getSchedName();
		this.jobName 	= quartzParameter.getJobName();
		this.jobGroup 	= quartzParameter.getJobGroup();
		this.jobClassName = quartzParameter.getJobClassName();
		this.description= quartzParameter.getDescription();
		this.isRecovery = quartzParameter.getIsRecovery()+"";
		this.triggerName= quartzParameter.getTriggerName();
		this.triggerGroup 	= quartzParameter.getTriggerGroup();
		this.isCronTrigger 	= quartzParameter.getIsCronTrigger()+"";
		this.expression 	= quartzParameter.getExpression();
		this.prevFireTime 	= DateStringUtils.DateToStr(quartzParameter.getPrevFireTime());
		this.nextFireTime 	= DateStringUtils.DateToStr(quartzParameter.getNextFireTime());
		this.startAt 		= DateStringUtils.DateToStr(quartzParameter.getStartAt());
		this.endAt 			= DateStringUtils.DateToStr(quartzParameter.getEndAt());

	    if("NONE".equals(quartzParameter.getStatus()))    
	    	this.status 		= "无状态";
	    else if("NORMAL".equals(quartzParameter.getStatus()))    
	    	this.status 		= "正常";
	    else if("PAUSED".equals(quartzParameter.getStatus()))    
	    	this.status 		= "暂停";
	    else if("COMPLETE".equals(quartzParameter.getStatus()))    
	    	this.status 		= "已完成";
	    else if("ERROR".equals(quartzParameter.getStatus()))    
	    	this.status 		= "运行出错";
	    else if("BLOCKED".equals(quartzParameter.getStatus()))    
	    	this.status 		= "被阻塞";
	    else
	    	this.status 		= "未知状态";
	    
	    this.cronDescription = CommonHandle.getExDes(this.expression);
		this.extraInfo 		= quartzParameter.getExtraInfo();
	}

	public String getSchedName() {
		return schedName;
	}

	public void setSchedName(String schedName) {
		this.schedName = schedName;
	}

	public String getJobName() {
		return jobName;
	}

	public void setJobName(String jobName) {
		this.jobName = jobName;
	}

	public String getJobGroup() {
		return jobGroup;
	}

	public void setJobGroup(String jobGroup) {
		this.jobGroup = jobGroup;
	}

	public String getJobClassName() {
		return jobClassName;
	}

	public void setJobClassName(String jobClassName) {
		this.jobClassName = jobClassName;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	
	public String getTriggerName() {
		return triggerName;
	}

	public void setTriggerName(String triggerName) {
		this.triggerName = triggerName;
	}

	public String getTriggerGroup() {
		return triggerGroup;
	}

	public void setTriggerGroup(String triggerGroup) {
		this.triggerGroup = triggerGroup;
	}

	

	public String getExpression() {
		return expression;
	}

	public void setExpression(String expression) {
		this.expression = expression;
	}

	public String getPrevFireTime() {
		return prevFireTime;
	}

	public void setPrevFireTime(String prevFireTime) {
		this.prevFireTime = prevFireTime;
	}

	public String getNextFireTime() {
		return nextFireTime;
	}

	public void setNextFireTime(String nextFireTime) {
		this.nextFireTime = nextFireTime;
	}

	public String getStartAt() {
		return startAt;
	}

	public void setStartAt(String startAt) {
		this.startAt = startAt;
	}

	public String getEndAt() {
		return endAt;
	}

	public void setEndAt(String endAt) {
		this.endAt = endAt;
	}

	public String getStatus() {
		return status;
	}

	public String getIsRecovery() {
		return isRecovery;
	}


	public void setIsRecovery(String isRecovery) {
		this.isRecovery = isRecovery;
	}


	public String getIsCronTrigger() {
		return isCronTrigger;
	}


	public void setIsCronTrigger(String isCronTrigger) {
		this.isCronTrigger = isCronTrigger;
	}


	public void setStatus(String status) {
		this.status = status;
	}

	public Map<String, String> getExtraInfo() {
		return extraInfo;
	}

	public void setExtraInfo(Map<String, String> extraInfo) {
		this.extraInfo = extraInfo;
	}
	

	public String getCronDescription() {
		return cronDescription;
	}


	public void setCronDescription(String cronDescription) {
		this.cronDescription = cronDescription;
	}


	public static List<QuartzModel> changeToQuartzModelList(List<QuartzParameter> quartzParameterList){
		List<QuartzModel> quartzModelList= new ArrayList<QuartzModel>();
		if(quartzParameterList == null) return quartzModelList;
		for(int i=0;i < quartzParameterList.size(); i ++){
			QuartzModel quartzModel = new QuartzModel(quartzParameterList.get(i));
			quartzModelList.add(quartzModel);
		}
		
		return quartzModelList;

	}

}
